'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    //AMD
    define(factory);
  } else if ((typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object') {
    //Node, CommonJS之类的
    module.exports = factory();
  } else {
    //浏览器全局变量(root 即 window)
    root.ResLoader = factory(root);
  }
})(this, function () {
  var isFunc = function isFunc(f) {
    return typeof f === 'function';
  };

  var ResLoader = function () {
    function ResLoader(config) {
      _classCallCheck(this, ResLoader);

      this.option = {
        resourceType: 'image', //资源类型，默认为图片
        baseUrl: './', //基准url
        resources: [], //资源路径数组
        onStart: null, //加载开始回调函数，传入参数total
        onProgress: null, //正在加载回调函数，传入参数currentIndex, total
        onComplete: null //加载完毕回调函数，传入参数total
      };

      this.option = Object.assign({}, this.option, config);

      this.status = 0; //加载器的状态，0：未启动   1：正在加载   2：加载完毕
      this.total = this.option.resources.length || 0; //资源总数
      this.currentIndex = 0; //当前正在加载的资源索引
    }

    _createClass(ResLoader, [{
      key: 'start',
      value: function start() {
        var _this2 = this;

        this.status = 1;
        var _this = this;
        var url = '';

        this.option.resources.forEach(function (item, index) {
          if (item.startsWith('http://') || item.startsWith('https://')) {
            url = item;
          } else {
            url = _this2.option.baseUrl + item;
          }

          var image = new Image();
          image.src = url;
          image.onload = function () {
            _this.loaded();
          };
          image.onerror = function () {
            _this.loaded();
          };
        });

        if (isFunc(this.option.onStart)) this.option.onStart(this.total);
      }
    }, {
      key: 'loaded',
      value: function loaded() {
        if (isFunc(this.option.onProgress)) {
          this.option.onProgress(++this.currentIndex, this.total);
        }
        //加载完毕
        if (this.currentIndex === this.total) {
          if (isFunc(this.option.onComplete)) {
            this.option.onComplete(this.total);
          }
        }
      }
    }]);

    return ResLoader;
  }();

  return ResLoader;
});
